package com.xjh.mange.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjh.mange.model.Log;
import com.xjh.mange.service.LogService;
import com.xjh.mange.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 徐佳辉
 * @date 2020/12/21 21:59
 */
@Controller
@RequestMapping("/log")
public class LogController {

    /**
     * 取前 10 条
     */
    public static final int INT = 10;
    @Autowired
    private LogService logService;

    /**
     * 取 10 条日志展示
     *
     * @return
     */
    @GetMapping("listTen")
    @ResponseBody
    public ResponseVO list() {
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        List<Log> list = logService.list(wrapper);
        if (list.size() < INT) {
            return ResponseVO.ok(list);
        } else {
            List<Log> collect = list.stream().limit(INT).collect(Collectors.toList());
            return ResponseVO.ok(collect);
        }
    }


}
